#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'HaiFeng'
__mtime__ = '2016/11/2'
"""
import time
from functools import wraps
import random
from datetime import datetime

#IntervalTypetemp = {'IntervalType.Tick': 'Tick','IntervalType.Second': '秒', 'IntervalType.Minute': '分', 'IntervalType.Hour': '时','IntervalType.Day': '天', 'IntervalType.Week': '周', 'IntervalType.Month': '月'}
IntervalTypetemp = {'Tick': 'Tick','Second': '秒', 'Minute': '分', 'Hour': '时','Day': '天', 'Week': '周', 'Month': '月'}
#----------------------------------------------------------
def fn_timer(function):
    '''
    装饰器 用于测试函数使用的时间
    :param function:
    :return:
    '''
    @wraps(function)
    def function_timer(*args,**kwargs):
        t0=time.time()
        result=function(*args,**kwargs)
        t1=time.time()
        print('函数%s运行使用了%s' %(function.__name__,str(t1-t0)))
        return result
    return function_timer

@fn_timer
def random_sort(n):
    #运行输出和不输出差距比较大 也就是尽量减少io操作
    #print(sorted([random.random() for i in range(n)]))
    sorted([random.random() for i in range(n)])


def customized_parse(s):
    """
    用于格式时间格式转换 字符串转换为时间元祖  替换这个函数 strptime
    :param s:
    :return:
    """
    s=s.ljust(21,'0')   #填充为21个字符 如果没有使用0代替
    if not s[4]=='-':    #'20190123 14:32:39'
        return datetime(int(s[0:4]), int(s[4:6]), int(s[6:8]), int(s[9:11]), int(s[12:14]), int(s[15:17]),
                    int(s[18:21]) * 1000)
    else:                 #'2018-05-14 21:00:0000'
        return datetime(int(s[0:4]), int(s[5:7]), int(s[8:10]), int(s[11:13]), int(s[14:16]), int(s[17:19]),
                    int(s[19:21]) * 1000)

def buwei(s):
    '''
    用于有些时间格式为个位数 201478  更改为20140708  保证数位正确
    :param s:
    :return:
    '''
    if len(str(s))==1:
        return '0'+str(s)
    else:
        return str(s)

#---------------------------------------------------------------------
def printLog(content):
    '''输出日志 非常好用'''
    t=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print ('%s\t%s' %(t,content))


#-------------------------------------------------------------------
# JSON配置文件路径
jsonPathDict = {}
def getJsonPath(name, moduleFile):
    """
    获取JSON配置文件的路径：
    1. 优先从当前工作目录查找JSON文件
    2. 若无法找到则前往模块所在目录查找
    使用方式
    settingFileName = 'DR_setting.json'
    settingFilePath = getJsonPath(settingFileName, __file__)
    """
    currentFolder = os.getcwd()
    currentJsonPath = os.path.join(currentFolder, name)
    if os.path.isfile(currentJsonPath):
        jsonPathDict[name] = currentJsonPath
        return currentJsonPath

    moduleFolder = os.path.abspath(os.path.dirname(moduleFile))
    moduleJsonPath = os.path.join(moduleFolder, '.', name)
    jsonPathDict[name] = moduleJsonPath
    return moduleJsonPath

if __name__ == "__main__":
    #random_sort(2000000)
    str1 = time.strftime('%Y%m%d %H:%M:%S', time.localtime())

    print(str1)
    ss='{0}{1}{2} {3}:{4}'.format(buwei(2017), buwei(9), buwei(8), buwei(9), buwei(9))
    print(customized_parse(ss))
    #print(customized_parse(ss))

#    customized_parse('{0}{1}{2} {3}:{4}'.format(2017, 9, 8, 22, 36))
    # tstr=datetime.strptime(str, '%Y%m%d %H:%M:%S')
    # tstr=tstr.strftime('%Y-%m-%d %H:%M:%S')



    #print(datetime.strftime(tstr,'%Y-%m-%d %H:%M:%S'))


    #print(str)


